package teach.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import teach.aistar.model.entity.*;
import teach.aistar.model.vo.Result;
import teach.aistar.service.*;
import teach.aistar.util.DateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class CustomerController {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private IOrderService iOrderService;

    @Autowired
   private IOrderDetailsService iOrderDetailsService;

    @Autowired
    private ICarDetailService iCarDetailService;

    @Autowired
    private IUserDetailService iUserDetailService;

    @Autowired
    private  ICarBandsService iCarBandsService;
    //客户信息显示
    @GetMapping("/customer")
    public String find(HttpServletRequest request, HttpSession session) {
        List<UserDetail> userDetails=iUserDetailService.selectDetailAll();

        System.out.println(userDetails);
//        model.addAttribute("userDetailList",userDetails);

         request.setAttribute("userDetailList",userDetails);

//        List<User> userList=iUserService.findAll();
//        session.setAttribute("userLists",userList);

        List<Orders> orders=iOrderService.selectOrderAll();
        request.setAttribute("ordersList",orders);

        List<OrdersDetails> ordersDetails=iOrderDetailsService.selectDeatilsAll();
        request.setAttribute("ordersDetail",ordersDetails);

        List<CarBrands> carBrands=iCarBandsService.selectAll();
        request.setAttribute("carBrandsList",carBrands);

        List<CarDetail> carDetails=iCarDetailService.selectAll();
        request.setAttribute("carDetailsList",carDetails);
        return "jsp/backend/page-list-customers";
    }

    //添加客户信息1
    @GetMapping("/addCustomer")
    public String addCustomer(HttpServletRequest request,HttpSession session) throws ParseException{

        return "jsp/app/user-add";
    }

    //添加客户信息
    @GetMapping("/addCustomer2")
    public String addCustomer2(HttpServletRequest request,HttpSession session) throws ParseException{
        String uname=request.getParameter("uname");
        String addaddress=request.getParameter("addaddress");
        System.out.println("aaaaaaaaaaaaaaaaaaa"+addaddress);
        String addCarId=request.getParameter("addCarId");
        String mobno=request.getParameter("mobno");
        String email=request.getParameter("email");

        String password=request.getParameter("password");
        String uphoto=request.getParameter("uphoto");

        UserDetail userDetail=new UserDetail();

        userDetail.setUphoto(uphoto);

        if (addaddress!=null){
            userDetail.setAddress(addaddress);
        }
        if (addCarId!=null){
            userDetail.setIdCard(addCarId);
        }
        if (mobno!=null){
            userDetail.setUphone(mobno);
        }
        if (email!=null){
            userDetail.setEmail(email);
        }
        if (uname!=null){
            userDetail.setUname(uname);
        }
        if (password!=null){
            userDetail.setUpwd(password);
        }
        iUserDetailService.insert(userDetail);

        return "redirect:/user/customer";
    }


    //删除客户信息
    @GetMapping("/deleteCustomer")
    @ResponseBody
    public Result deleteCustomer(HttpServletRequest request, HttpSession session) {
       int cid = Integer.valueOf(request.getParameter("id"));
        iUserDetailService.deleteByCuId(cid);
        return new Result("200");
    }

    //客户多条件查询
    @GetMapping("/selectCustomer")
    public String selectCustomer (HttpServletRequest request, HttpSession session) throws ParseException {
        String uname=request.getParameter("customer");
        String address=request.getParameter("address");
        String utime=request.getParameter("year");

        UserDetail userDetail=new UserDetail();
        if (uname!=null && uname.trim().length()>0){
            userDetail.setUname(uname);
        }
        if (address!=null && address.trim().length()>0){
            userDetail.setAddress(address);
        }
        if (utime!=null && !utime.equals("")){
            Date date=DateUtil.stringToDate(utime);
            userDetail.setUtime(date);
        }

        List<UserDetail> userDetails=iUserDetailService.findByConditional(userDetail);
        System.out.println(userDetails.size()+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        //条件回显
        request.setAttribute("customer",uname);
        request.setAttribute("address",address);
        request.setAttribute("year",utime);

        request.setAttribute("userDetailList",userDetails);
        return "jsp/backend/page-list-customers";
    }

    //编辑客户信息
    @GetMapping("/editCustomer")
    public String editCustomer (HttpServletRequest request, HttpSession session) {
//        int id=Integer.parseInt(request.getParameter("id"));
//
//        List<UserDetail> userDetails=iUserDetailService.selectDetailAll();
//        UserDetail userDetail=iUserDetailService.selectById(id);
//        request.setAttribute("userDetailList",userDetails);
//
//        String fname = (String) request.getParameter("fname");
//        String addaddress = (String) request.getParameter("addaddress");
//        String addCarId = (String) request.getParameter("addCarId");
//        String mobno = (String) request.getParameter("mobno");
//        String email = (String) request.getParameter("email");
//
//        String uname = (String) request.getParameter("uname");
//        String password = (String) request.getParameter("password");
//
//        if (fname != null){
//            userDetail.setUname(fname);
//        }
//        if (addaddress != null){
//            userDetail.setAddress(addaddress);
//        }
//        if (addCarId != null){
//            userDetail.setIdCard(addCarId);
//        }
//        if (mobno != null){
//            userDetail.setUphone(mobno);
//        }
//        if (email != null){
//            userDetail.setEmail(email);
//        }
//        if (uname != null){
//            userDetail.setUname(uname);
//        }
//        if (password != null){
//            userDetail.setUpwd(password);
//        }
//
//        iUserDetailService.updateCustomer(userDetail);
        return "/jsp/backend/page-edit-customers";
    }

}
